#include"Maze.h"

void Maze::Stack::Push(TypeData x)
{
	if (_size + 1 >= _capacity)
	{
		int newcapacity = 2 * _capacity;
		TypeData* tmp = (TypeData*)realloc(_a, newcapacity*sizeof(TypeData));
		assert(tmp);
		_a = tmp;
		_capacity = newcapacity;
	}
	
	_a[++_size]._x = x._x;
	_a[_size]._y = x._y;
}

void Maze::Print()
{
	for (int i = 0; i < _rows; i++)
	{
		for (int j = 0; j < _cols; j++)
		{
			cout << _pool[i][j] << " ";
		}
		cout << endl;
	}
}

bool Maze::IsNodeTure(int x,int y)
{
	if (y >= 0 && y < _rows
		&& x >= 0 && x < _cols
		&& _pool[y][x] == 0)
	{
		return 1;
	}
	else
		return 0;
}

bool Maze::GetPassage(int x,int y)
{
	Node node(x, y);
	
	_stack.Push(node);
	if (x == _cols - 1 && y == _rows - 1)
		return 1;
	_pool[y][x] = -1;
	//up
	if (IsNodeTure(x,y - 1))
	{
		if (GetPassage(x, y - 1))
			return 1;
	}
	//down
	if (IsNodeTure(x,y + 1))
	{
		if (GetPassage(x, y + 1))
			return 1;
	}
	//left
	if (IsNodeTure(x - 1,y))
	{
		if (GetPassage(x - 1, y))
			return 1;
	}
	//right
	if (IsNodeTure(x + 1,y))
	{
		if (GetPassage(x + 1, y))
			return 1;
	}
	_stack.Pop();
	return 0;
}

